<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-big-counter.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"raphets.github.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="目前主要的图片加载框架有Picasso、Glide、Fresco等，本文以Picasso为例，总结一些图片加载的内存优化问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="Picasso 加载图片之内存优化">
<meta property="og:url" content="https://raphets.github.io/archives/5dd816c0.html">
<meta property="og:site_name" content="尚拙">
<meta property="og:description" content="目前主要的图片加载框架有Picasso、Glide、Fresco等，本文以Picasso为例，总结一些图片加载的内存优化问题。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-05-09T02:52:56.000Z">
<meta property="article:modified_time" content="2022-05-25T02:42:58.024Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Picasso">
<meta property="article:tag" content="内存优化">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://raphets.github.io/archives/5dd816c0.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Picasso 加载图片之内存优化 | 尚拙</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">尚拙</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">一个分享技术、学习成长的个人博客网站</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tool">

    <a href="/tool/" rel="section"><i class="fa fa-hammer fa-fw"></i>工具</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://raphets.github.io/archives/5dd816c0.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="John Doe">
      <meta itemprop="description" content="不积跬步，无以至千里；不积小流，无以成江海。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="尚拙">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Picasso 加载图片之内存优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-05-09 10:52:56" itemprop="dateCreated datePublished" datetime="2019-05-09T10:52:56+08:00">2019-05-09</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-05-25 10:42:58" itemprop="dateModified" datetime="2022-05-25T10:42:58+08:00">2022-05-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/" itemprop="url" rel="index"><span itemprop="name">技术文章</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>目前主要的图片加载框架有Picasso、Glide、Fresco等，本文以Picasso为例，总结一些图片加载的内存优化问题。</p>
<span id="more"></span>

<h1 id="1、裁减图片"><a href="#1、裁减图片" class="headerlink" title="1、裁减图片"></a>1、裁减图片</h1><p>在列表页尽量使用裁剪后的图片，在查看大图模式下才加载完整的图片。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Picasso.with(mContext)</span><br><span class="line">.load(url)</span><br><span class="line">.resize(dp2px(250),dp2px(250))</span><br><span class="line">.centerCrop()</span><br><span class="line">.into(imageView);</span><br></pre></td></tr></table></figure>

<h1 id="2、加载大图不使用memory-cache"><a href="#2、加载大图不使用memory-cache" class="headerlink" title="2、加载大图不使用memory cache"></a>2、加载大图不使用memory cache</h1><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Picasso.with(mContext)</span><br><span class="line">.load(mURL)</span><br><span class="line">.memoryPolicy(NO_CACHE, NO_STORE)</span><br><span class="line">.into(imageView);</span><br></pre></td></tr></table></figure>

<h1 id="3、重写ImageView加速内存回收"><a href="#3、重写ImageView加速内存回收" class="headerlink" title="3、重写ImageView加速内存回收"></a>3、重写ImageView加速内存回收</h1><p>重写ImageView的onDetachedFromWindow方法，在它从屏幕中消失时回调，去掉drawable引用，能加快内存的回收。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">public class RecyclerImageView extends ImageView</span><br><span class="line">&#123; </span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    @Override    </span><br><span class="line">    protected void onDetachedFromWindow() &#123;</span><br><span class="line">        super.onDetachedFromWindow();</span><br><span class="line">        setImageDrawable(null);   </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="4、新进程中查看大图"><a href="#4、新进程中查看大图" class="headerlink" title="4、新进程中查看大图"></a>4、新进程中查看大图</h1><p>列表页的内存已经非常稳定，但是查看大图时，大图往往占用了20+m内存，加上现有进程中的内存，非常容易oom，在新进程中打开Activity成为比较取巧的避免oom的方式。只要在AndroidManifest.xml中定义Activity时加入process属性，即可在新进程中打开此Activity。由此，picasso也将在新进程中创建基于新ApplicationContext的单例。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;activity android:name=&quot;.DetailActivity&quot; android:process=&quot;:picture&quot;/&gt;</span><br></pre></td></tr></table></figure>

<h1 id="5、列表滑动优化"><a href="#5、列表滑动优化" class="headerlink" title="5、列表滑动优化"></a>5、列表滑动优化</h1><p>picasso可以对多个加载请求设置相同的tag：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Object tag = new Object();</span><br><span class="line"></span><br><span class="line">Picasso.with( imageView.getContext() )</span><br><span class="line">.load(url)</span><br><span class="line">.resize(dp2px(250),dp2px(250))</span><br><span class="line">.centerCrop()</span><br><span class="line">.tag(tag)</span><br><span class="line">.into(imageView);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>在RecyclerView滑动时监听，处理不同的表现：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()&#123;</span><br><span class="line">    @Override</span><br><span class="line">    public void onScrollStateChanged(RecyclerView recyclerView, int newState)</span><br><span class="line">    &#123;</span><br><span class="line">        if (newState == RecyclerView.SCROLL_STATE_IDLE)</span><br><span class="line">        &#123;</span><br><span class="line">            Picasso.with(context).resumeTag(tag);</span><br><span class="line">        &#125;</span><br><span class="line">        else</span><br><span class="line">        &#123;</span><br><span class="line">            Picasso.with(context).pauseTag(tag);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h1 id="6、降低图片质量"><a href="#6、降低图片质量" class="headerlink" title="6、降低图片质量"></a>6、降低图片质量</h1><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">Picasso.with( imageView.getContext() )</span><br><span class="line">.load(url)</span><br><span class="line">.config(Bitmap.Config.RGB_565)</span><br><span class="line">.into(imageView);</span><br></pre></td></tr></table></figure>

<p>默认情况下，Android使用ARGB_8888</p>
<p>Android中有四种，分别是：</p>
<p>ALPHA_8：每个像素占用1byte内存</p>
<p>ARGB_4444:每个像素占用2byte内存</p>
<p>ARGB_8888:每个像素占用4byte内存</p>
<p>RGB_565:每个像素占用2byte内存</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Picasso/" rel="tag"><i class="fa fa-tag"></i> Picasso</a>
              <a href="/tags/%E5%86%85%E5%AD%98%E4%BC%98%E5%8C%96/" rel="tag"><i class="fa fa-tag"></i> 内存优化</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/archives/fbc16b6.html" rel="prev" title="Java 集合排序总结">
      <i class="fa fa-chevron-left"></i> Java 集合排序总结
    </a></div>
      <div class="post-nav-item">
    <a href="/archives/bb162381.html" rel="next" title="『学习之道』读书笔记">
      『学习之道』读书笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1%E3%80%81%E8%A3%81%E5%87%8F%E5%9B%BE%E7%89%87"><span class="nav-number">1.</span> <span class="nav-text">1、裁减图片</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2%E3%80%81%E5%8A%A0%E8%BD%BD%E5%A4%A7%E5%9B%BE%E4%B8%8D%E4%BD%BF%E7%94%A8memory-cache"><span class="nav-number">2.</span> <span class="nav-text">2、加载大图不使用memory cache</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3%E3%80%81%E9%87%8D%E5%86%99ImageView%E5%8A%A0%E9%80%9F%E5%86%85%E5%AD%98%E5%9B%9E%E6%94%B6"><span class="nav-number">3.</span> <span class="nav-text">3、重写ImageView加速内存回收</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4%E3%80%81%E6%96%B0%E8%BF%9B%E7%A8%8B%E4%B8%AD%E6%9F%A5%E7%9C%8B%E5%A4%A7%E5%9B%BE"><span class="nav-number">4.</span> <span class="nav-text">4、新进程中查看大图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5%E3%80%81%E5%88%97%E8%A1%A8%E6%BB%91%E5%8A%A8%E4%BC%98%E5%8C%96"><span class="nav-number">5.</span> <span class="nav-text">5、列表滑动优化</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6%E3%80%81%E9%99%8D%E4%BD%8E%E5%9B%BE%E7%89%87%E8%B4%A8%E9%87%8F"><span class="nav-number">6.</span> <span class="nav-text">6、降低图片质量</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="John Doe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">John Doe</p>
  <div class="site-description" itemprop="description">不积跬步，无以至千里；不积小流，无以成江海。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">47</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">John Doe</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
